package Game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class TicTacToeField extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1103882512125844240L;
	public static final int fields = 3;
	public static final int gap = 1;
	public static final int size = GameField.size * fields + (fields + 1) * gap + 4;
	
	private int x;
	private int y;

	public TicTacToeField(int x ,int y) {
		this.x = x;
		this.y = y;
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		this.setLayout(new FlowLayout(FlowLayout.LEFT, gap, gap));
		this.setMaximumSize(new Dimension(size, size));
		this.setMinimumSize(new Dimension(size, size));
		this.setPreferredSize(new Dimension(size, size));
		for (int yy = 0; yy < fields; yy++) {
			for (int xx = 0; xx < fields; xx++) {
				this.add(new GameField(x, y, xx, yy));
			}
		}
	}
	
	public void paintComponent(java.awt.Graphics g) {
		super.paintComponent(g);
	}
	
	public void paintChildren(java.awt.Graphics g) {
		super.paintChildren(g);
		Game.FieldState state = Main.getFieldState(x, y);
		Player.Sign yourSign = Player.getPlayerByID(Player.PlayerID.you).getSign();
		if (state != null && yourSign != null) {
			if (state == Game.FieldState.selectable) {
				g.setColor(new Color(0x400000FF, true));
			} else if ((state == Game.FieldState.won && yourSign == Player.Sign.circle) 
					   || (state == Game.FieldState.lost && yourSign == Player.Sign.cross)) {
				g.setColor(new Color(0x4000FF00, true));
			} else {
				g.setColor(new Color(0x40FF0000, true));
			}
			g.fillRect(0, 0, size, size);
		}
	}

}
